package com.sskj.qiangjin.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.model.repository.UserRepository;
import com.sskj.qiangjin.bean.BackUserBean;
import com.sskj.qiangjin.http.HttpConfig;
import com.sskj.qiangjin.ui.activity.LoginActivity;

/**
 * Created by QinKang on 2018/12/29.
 */

public class LoginActivityPresenter extends BasePresenter<LoginActivity> {
    /**
     * 登录
     *
     * @param mobile 手机号
     * @param opwd   密码
     * @return
     */
    public void login(String mobile, String opwd) {
        httpService.login(mobile, opwd)
                .execute(new JsonCallBack<HttpData<BackUserBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<BackUserBean>> response) {
                        HttpData<BackUserBean> httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            SPUtil.put(SPConfig.ACCOUNT, httpData.getData().getAccount());
                            SPUtil.put(SPConfig.TOKEN, httpData.getData().getToken());
                            HttpHeaders headers = new HttpHeaders();
                            headers.put("systemType", "android");
                            headers.put("account", httpData.getData().getAccount());    //所有的 header 都 不支持 中文
                            headers.put("token", httpData.getData().getToken());
                            HttpParams httpParams = new HttpParams();
                            httpParams.put("systemType", "android");
                            httpParams.put("account", httpData.getData().getAccount());    //所有的 header 都 不支持 中文
                            httpParams.put("token", httpData.getData().getToken());
                            OkGo.getInstance().addCommonHeaders(headers);
                            OkGo.getInstance().addCommonParams(httpParams);
                            mView.loginSuccess(httpData.getData());
                        }
                    }
                });
    }

}
